package com.ltkj.shop.controller.mobile;

import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.web.page.TableDataInfo;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.shop.domain.ShopCart;
import com.ltkj.shop.domain.vo.ShopCartVo;
import com.ltkj.shop.service.IShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 文创购物车移动端Controller
 * 
 * @author xupf
 * @date 2023-08-31
 */
@Api(tags = "文创购物车移动端接口")
@RestController
@RequestMapping("/mobile/cart")
public class ShopCartMobileController extends BaseController
{
    @Autowired
    private IShopCartService shopCartService;

    /**
     * 查询文创购物车列表
     */
    @ApiOperation("查询文创购物车列表")
    @GetMapping("/list")
    public TableDataInfo list(ShopCart shopCart)
    {
        startPage();
        shopCart.setUserId(SecurityUtils.getUserId());
        List<ShopCartVo> list = shopCartService.selectList(shopCart);
        return getDataTable(list);
    }

    /**
     * 统计当前用户购物车数量
     */
    @ApiOperation("统计当前用户购物车数量")
    @GetMapping("/countByUser")
    public AjaxResult countByUser()
    {
        Long count = shopCartService.countByUser();
        return success(count);
    }

    /**
     * 获取文创购物车详细信息
     */
    @ApiOperation("获取文创购物车详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shopCartService.selectShopCartById(id));
    }

    /**
     * 新增文创购物车
     */
    @ApiOperation("新增文创购物车")
    @Log(title = "文创购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopCart shopCart)
    {
        return shopCartService.insertShopCart(shopCart);
    }

    /**
     * 修改文创购物车
     */
    @ApiOperation("修改文创购物车")
    @Log(title = "文创购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopCart shopCart)
    {
        return toAjax(shopCartService.updateShopCart(shopCart));
    }

    /**
     * 清空购物车
     */
    @ApiOperation("清空购物车")
    @Log(title = "文创购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/clearCart")
    public AjaxResult clearCart(Long[] goodsIds, String[] types, Long userId)
    {
        if (Objects.isNull(userId)) {
            userId = SecurityUtils.getLoginUser().getUserid();
        }
        return shopCartService.deleteByUser(userId, goodsIds, types);
    }

    /**
     * 删除文创购物车
     */
    @ApiOperation("删除文创购物车")
    @Log(title = "文创购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shopCartService.deleteShopCartByIds(ids));
    }
}
